package com.yty.system.oauth.config.jwt;

import com.yty.system.oauth.config.exception.AuthExceptionEntryPoint;
import com.yty.system.oauth.config.exception.CustomAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
@EnableResourceServer
public class ResourceServiceConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    private AuthExceptionEntryPoint authExceptionEntryPoint;
    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                //.anonymous().disable()  //匿名访问
                .antMatcher("/**")        //匹配需要资源认证路径
                .authorizeRequests()
//                .antMatchers("/user/**","/oauth/**").permitAll()  //匹配不需要资源认证路径 需要放过的接口
                .anyRequest().authenticated()
                .and()
                .exceptionHandling() //添加filter
                .accessDeniedHandler(customAccessDeniedHandler)  //异常处理
                .authenticationEntryPoint(authExceptionEntryPoint);   //认证异常流程
    }
}
